package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.MedicineConstants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.PurchaseOrderMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 采购管理Service业务层处理
 *
 * @author yangxp
 * @date 2020-07-31
 */
@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements IPurchaseOrderService {
	@Resource
	private IMedPoService medPoService;
	@Resource
	private IMedStockRecordService medStockRecordService;
	@Resource
	private IMedicineService medicineService;
	@Resource
	private IMedPriceRecordService medPriceRecordService;

	@Override
	@Transactional(rollbackFor = BaseException.class)
	public boolean savePurchaseOrder(PurchaseOrder purchaseOrder) {

		List<MedPo> medicineList = purchaseOrder.getMedicineList();

		if (CollectionUtil.isEmpty(medicineList)) {
			return false;
		}
		String poId = IdUtil.fastSimpleUUID();
		medicineList.forEach(item -> {
			Medicine medicine = medicineService.getOne(new LambdaQueryWrapper<Medicine>().eq(Medicine::getMedId, item.getMedId()));
			String medName = medicine.getMedName();
			BigDecimal medicineStock = medicine.getStock();
			//重量 千克 -> 克
			BigDecimal kgWeight = item.getMedWeight();
			BigDecimal gWeight = NumberUtil.mul(kgWeight, 1000);
			item.setMedWeight(gWeight);
			item.setPoId(poId);
			item.setGmtCreate(DateUtil.date());
			item.setGmtModified(DateUtil.date());
			item.setMedName(medName);

			//保存库存记录表
			MedStockRecord medStockRecord = new MedStockRecord();
			medStockRecord.setMsrId(IdUtil.fastSimpleUUID());
			medStockRecord.setPoId(poId);
			medStockRecord.setMedId(item.getMedId());
			medStockRecord.setMedName(medName);
			medStockRecord.setChangeType(MedicineConstants.CHANGE_TYPE_ADD);
			medStockRecord.setOldStock(medicineStock);
			medStockRecord.setChangeAmount(gWeight);
			medStockRecord.setNewStock(NumberUtil.add(gWeight, medicineStock));
			medStockRecord.setGmtCreate(DateUtil.date());
			medStockRecord.setGmtModified(DateUtil.date());
			medStockRecordService.save(medStockRecord);

			//保存价格记录
			MedPriceRecord medPriceRecord = new MedPriceRecord();
			BigDecimal newCostPrice = item.getMedCostPrice();
			BigDecimal oldCostPrice = medicine.getCostPrice();
			BigDecimal newMarketPrice = item.getMedMarketPrice();
			BigDecimal oldMarketPrice = medicine.getMarketPrice();

			//比较进价
			int costPriceCompare = NumberUtil.compare(newCostPrice.doubleValue(), oldCostPrice.doubleValue());
			//比较售价
			int marketPriceCompare = NumberUtil.compare(newMarketPrice.doubleValue(), oldMarketPrice.doubleValue());

			//如果现在进价大于以前
			if (costPriceCompare==1) {
				medPriceRecord.setCostPriceChangeAmount(NumberUtil.sub(newCostPrice, oldCostPrice));
				medPriceRecord.setCostPriceChangeType(MedicineConstants.CHANGE_TYPE_ADD);
			} else if (costPriceCompare==-1){

				medPriceRecord.setCostPriceChangeAmount(NumberUtil.sub(oldCostPrice, newCostPrice));
				medPriceRecord.setCostPriceChangeType(MedicineConstants.CHANGE_TYPE_SUB);
			}else {
				medPriceRecord.setCostPriceChangeAmount(NumberUtil.sub(oldCostPrice, newCostPrice));
				medPriceRecord.setCostPriceChangeType(MedicineConstants.CHANGE_TYPE_NO_CHANGE);
			}
			//如果现在售价大于以前
			if (marketPriceCompare==1) {
				medPriceRecord.setMarketPriceChangeAmount(NumberUtil.sub(newMarketPrice, oldMarketPrice));
				medPriceRecord.setMarketPriceChangeType(MedicineConstants.CHANGE_TYPE_ADD);
			} else if (marketPriceCompare==-1){
				medPriceRecord.setMarketPriceChangeAmount(NumberUtil.sub(oldMarketPrice, newMarketPrice));
				medPriceRecord.setMarketPriceChangeType(MedicineConstants.CHANGE_TYPE_SUB);
			}else {
				medPriceRecord.setMarketPriceChangeAmount(NumberUtil.sub(oldMarketPrice, newMarketPrice));
				medPriceRecord.setMarketPriceChangeType(MedicineConstants.CHANGE_TYPE_NO_CHANGE);
			}
			medPriceRecord.setMedId(item.getMedId());
			medPriceRecord.setMedName(medName);
			medPriceRecord.setMprId(IdUtil.fastSimpleUUID());
			medPriceRecord.setOldCostPrice(oldCostPrice);
			medPriceRecord.setNewCostPrice(newCostPrice);
			medPriceRecord.setOldMarketPrice(oldMarketPrice);
			medPriceRecord.setNewMarketPrice(newMarketPrice);
			medPriceRecord.setGmtCreate(DateUtil.date());
			medPriceRecord.setGmtModified(DateUtil.date());
			medPriceRecordService.save(medPriceRecord);

			//更新药品库存
			medicine.setStock(NumberUtil.add(medicineStock, gWeight));
			medicine.setCostPrice(newCostPrice);
			medicine.setMarketPrice(newMarketPrice);
			medicine.setGmtModified(DateUtil.date());
			medicineService.updateById(medicine);
		});
		boolean saveBatch = medPoService.saveBatch(medicineList);
		// 获取当前的用户信息
		LoginUser currentUser = SecurityUtils.getLoginUser();
		// 获取当前的用户名称
		String userName = currentUser.getUsername();
		purchaseOrder.setPoId(poId);
		purchaseOrder.setPoCreator(userName);
		purchaseOrder.setGmtCreate(DateUtil.date());
		purchaseOrder.setGmtModified(DateUtil.date());
		boolean save = this.save(purchaseOrder);
		return save && saveBatch;
	}
}
